<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace Shop\Api;
use Common\Api\Api;
use Shop\Model\CategoryPropModel;

class CategoryPropApi extends Api{


    /**
     * 查询,不分页
     */
    const QUERY_NO_PAGING = "Shop/CategoryProp/queryNoPaging";
    /**
     * 添加
     */
    const ADD = "Shop/CategoryProp/add";
    /**
     * 保存
     */
    const SAVE = "Shop/CategoryProp/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Shop/CategoryProp/saveByID";

    /**
     * 删除
     */
    const DELETE = "Shop/CategoryProp/delete";

    /**
     * 查询
     */
    const QUERY = "Shop/CategoryProp/query";
    /**
     * 查询一条数据
     */
    const GET_INFO = "Shop/CategoryProp/getInfo";
    /**
     * 查询类目属性的值表
     */
    const QUERY_PROP_TABLE = "Shop/CategoryProp/queryPropTable";


    /**
     * 查询国别
     */
    const QUERY_LANG = "Shop/CategoryProp/queryLang";

	protected function _init(){
		$this->model = new CategoryPropModel();	
	}

	public function addCatePropLang($prop_id,$cate_id,$langs,$langs_value){

		$allEntity = [];

		for($i = 0 ;$i<count($langs);$i++){
			array_push($allEntity,[
				'propid'   => $prop_id,
				'cate_id'  => $cate_id,
				'propname' => $langs_value[$i],
				'lang'     => $langs[$i]
			]);
		}

		$result = $this->getModel()->addAll($allEntity);

		return $result;
	}

	public function updateCatePropLang($prop_id,$cate_id,$langs,$langs_value){

        for($i = 0 ;$i<count($langs);$i++){
            $updateEntity = [
                'propname' => $langs_value[$i]
            ];

            $map = ['propid'   => $prop_id,'lang'     => $langs[$i]];
            $result = $this->save($map,$updateEntity);
            if(!$result['status']){
                return $result;
            }
        }

        return $result;
	}
	
    /**
 * 查询属性
 * @param $map
 * @return array
 */
    public function queryPropTable($map){

        $result = $this->model->where($map)->select();
        if($result === false){
            return $this->apiReturnErr($this->model->getError());
        }

        $propvalueApi = new CategoryPropvalueApi();
        $return = array();
        foreach($result as $prop){
            $one = array(
                'id'=>$prop['id'],
                'propid'=>$prop['propid'],
                'lang'=>$prop['lang'],
                'name'=>$prop['propname'],
                'property_value'=>array()
            );
            $map = array('prop_id'=>$prop['id']);
            $propvalue = $propvalueApi->queryNoPaging($map);
            if($propvalue['status']){
                $one['property_value'] = $propvalue['info'];
            }else{
                return $this->apiReturnErr($propvalue['info']);
            }
            array_push($return,$one);
        }
        return $this->apiReturnSuc($return);

    }
    /**
     * 查询国别
     * @param $map
     * @return array
     */
    public function queryLang(){

        $result = $this->model
            ->field('lang')
            ->group('lang')
            ->select();
        return $result;
    }
	
}


